package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.vo.PasswordVo;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SuperVipPointMapper;
import com.ruoyi.system.domain.SuperVipPoint;
import com.ruoyi.system.service.ISuperVipPointService;

import javax.annotation.Resource;

/**
 * 用户VIP成长值Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-14
 */
@Service
public class SuperVipPointServiceImpl implements ISuperVipPointService
{
    @Autowired
    private SuperVipPointMapper superVipPointMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 查询用户VIP成长值
     * 
     * @param id 用户VIP成长值主键
     * @return 用户VIP成长值
     */
    @Override
    public SuperVipPoint selectSuperVipPointById(Long id)
    {
        return superVipPointMapper.selectSuperVipPointById(id);
    }

    /**
     * 查询用户VIP成长值列表
     * 
     * @param superVipPoint 用户VIP成长值
     * @return 用户VIP成长值
     */
    @Override
    public List<SuperVipPoint> selectSuperVipPointList(SuperVipPoint superVipPoint)
    {
        return superVipPointMapper.selectSuperVipPointList(superVipPoint);
    }

    /**
     * 新增用户VIP成长值
     * 
     * @param superVipPoint 用户VIP成长值
     * @return 结果
     */
    @Override
    public int insertSuperVipPoint(SuperVipPoint superVipPoint)
    {
        return superVipPointMapper.insertSuperVipPoint(superVipPoint);
    }

    /**
     * 修改用户VIP成长值
     * 
     * @param superVipPoint 用户VIP成长值
     * @return 结果
     */
    @Override
    public int updateSuperVipPoint(SuperVipPoint superVipPoint)
    {
        return superVipPointMapper.updateSuperVipPoint(superVipPoint);
    }

    /**
     * 批量删除用户VIP成长值
     * 
     * @param ids 需要删除的用户VIP成长值主键
     * @return 结果
     */
    @Override
    public int deleteSuperVipPointByIds(Long[] ids)
    {
        return superVipPointMapper.deleteSuperVipPointByIds(ids);
    }

    /**
     * 删除用户VIP成长值信息
     * 
     * @param id 用户VIP成长值主键
     * @return 结果
     */
    @Override
    public int deleteSuperVipPointById(Long id)
    {
        return superVipPointMapper.deleteSuperVipPointById(id);
    }

    @Override
    // 获取用户VIP成长值
    public SuperVipPoint getUserVipPoint(Long userId) {
        return superVipPointMapper.getUserVipPoint(userId);
    }

    @Override
    public int updatePassword(PasswordVo passwordVo) {
        // 判断旧密码是否一致
        SysUser sysUser = sysUserMapper.selectUserById(passwordVo.getUserId());
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        boolean isMatch = encoder.matches(passwordVo.getOldPassword(),sysUser.getPassword());
        if (!isMatch) {
            throw new RuntimeException("原密码验证错误");
        }
        // 密码加密
        passwordVo.setNewPassword(SecurityUtils.encryptPassword(passwordVo.getNewPassword()));
        // 修改密码
        return superVipPointMapper.updatePassword(passwordVo);
    }
}
